package com.qxsx.midi.activity.study;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.qxsx.midi.R;
import com.qxsx.midi.activity.base.BaseActivity;
import com.qxsx.midi.activity.exam.MidiTeacherActivity;
import com.qxsx.midi.activity.home.BuyListActivity;
import com.qxsx.midi.activity.me.MeKejiantActivity;
import com.qxsx.midi.config.AppContext;
import com.qxsx.midi.utils.MyNet;
import com.qxsx.midi.utils.callBackJsonArray;
import com.qxsx.midi.view.CircleImageView;

import net.tsz.afinal.FinalBitmap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Administrator on 2015/9/7.
 */
public class MidiManDetailsActivity extends Activity implements OnClickListener {

    private TextView tv_name;//名字
    private TextView tv_id;//ID
    private CircleImageView img_icon;//头像
    private TextView tv_gender;//性别
    private TextView tv_major;//主修
    private TextView tv_position;//位置
    private TextView tv_maxlevel;//最高级别
    private TextView tv_period;//认证周期
    private TextView tv_certificate;//证书编号
    private TextView tv_organization;//培训机构
    private TextView text_about;//迷笛先生简介
    private String _id;
    private String _tel;
    private String weixin_num = "";
    private String name = "";
    private FinalBitmap finalImageLoader;

    private LinearLayout ll_back, ll_phone, ll_weixin;

    Dialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_study_man_detail);
        Intent intent = getIntent();
        _id = intent.getStringExtra("id");
        findById();
        inItData();
        finalImageLoader = FinalBitmap.create(this);
        finalImageLoader.configLoadingImage(R.mipmap.binner);
        finalImageLoader.configLoadfailImage(R.mipmap.binner);

    }


    private void inItData() {
        JSONArray array = new JSONArray();
        JSONObject object = new JSONObject();

        try {
            object.put("mr_id", _id + "");
            array.put(object);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        MyNet.doPostJsonArray("604", array, new callBackJsonArray() {
            @Override
            public void run(Object code, Object message, JSONArray array) {
                try {

                    if ("200".equals(code.toString())) {
                        JSONObject object = array.getJSONObject(0);
                        tv_name.setText(object.getString("real_name"));
                        tv_id.setText(object.getString("midi_id"));
                        if ("1".equals(object.getString("sex"))) {
                            tv_gender.setText("男");
                        } else {
                            tv_gender.setText("女");
                        }
                        tv_major.setText(object.getString("major_name"));
                        tv_position.setText(object.getString("name"));
                        tv_maxlevel.setText(object.getString("highest_level"));
                        tv_period.setText(object.getString("agree_cycle"));
                        tv_certificate.setText(object.getString("certificate_num"));
                        tv_organization.setText(object.getString("belong_institution"));
                        text_about.setText(object.getString("mr_introduce"));
                        name = object.getString("real_name");
                        weixin_num = object.getString("weixin_num");
                        _tel = object.getString("phone");
                        MyNet.setNetImage(getApplicationContext(), img_icon, object.getString("head_img"));
                    } else {
                        Log.e("code", code.toString());
                        Log.e("message", message.toString());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

    }

    private void findById() {
        tv_name = (TextView) findViewById(R.id.text_name);
        tv_id = (TextView) findViewById(R.id.text_id);
        tv_gender = (TextView) findViewById(R.id.text_gender);
        tv_major = (TextView) findViewById(R.id.text_major);
        tv_position = (TextView) findViewById(R.id.text_position);
        tv_maxlevel = (TextView) findViewById(R.id.text_level);
        tv_period = (TextView) findViewById(R.id.text_period);
        tv_certificate = (TextView) findViewById(R.id.text_certificate);
        tv_organization = (TextView) findViewById(R.id.text_organization);
        img_icon = (CircleImageView) findViewById(R.id.image_icon);
        text_about = (TextView) findViewById(R.id.text_about);


        ll_back = (LinearLayout) findViewById(R.id.ll_back);
        ll_phone = (LinearLayout) findViewById(R.id.ll_phone);
        ll_weixin = (LinearLayout) findViewById(R.id.ll_weixin);

        ll_back.setOnClickListener(this);
        ll_phone.setOnClickListener(this);
        ll_weixin.setOnClickListener(this);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.ll_back:
                finish();
                break;
            case R.id.ll_phone:
                popWindow();
                break;
            case R.id.ll_weixin:
                LayoutInflater layout = LayoutInflater.from(AppContext.getContext());
                View view = layout.inflate(R.layout.diago_weixin, null);
                dialog = new AlertDialog.Builder(MidiManDetailsActivity.this).create();
                TextView tv_weixin = (TextView) view.findViewById(R.id.tv_weixin);
                tv_weixin.setText(name + ":" + weixin_num);
                TextView tv_sure = (TextView) view.findViewById(R.id.tv_sure);
                tv_sure.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });
                DisplayMetrics dm = new DisplayMetrics();
                getWindowManager().getDefaultDisplay().getMetrics(dm);
                int x = dm.widthPixels;// 屏幕的宽
                int y = dm.heightPixels;// 屏幕的高
                dialog = new Dialog(MidiManDetailsActivity.this, R.style.dialog_finish);
                dialog.getWindow().setLayout((int) (x / 2.2), (int) (y / 1.8));
                dialog.show();
                dialog.setContentView(view);
                dialog.setCancelable(false);
                break;

            default:

                break;

        }
    }

    PopupWindow popupWindow;

    public void popWindow() {
        LayoutInflater mLayoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        View menuView = mLayoutInflater.inflate(R.layout.fragment_study_call_menu, null, true);       //弹出窗口包含的视图
        popupWindow = new PopupWindow(menuView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, true); //创建弹出窗口，指定大小
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        menuView.setFocusableInTouchMode(true);
        menuView.setFocusable(true);
        popupWindow.setBackgroundDrawable(getResources().getDrawable(R.mipmap.study_call_menu_bg));
        menuView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (popupWindow.isShowing()) {
                    popupWindow.dismiss();
                }
            }
        });
        menuView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
                if (arg1 == KeyEvent.KEYCODE_BACK) {
                    if (popupWindow != null) {
                        popupWindow.dismiss();
                    }
                }
                return false;
            }
        });
        popupWindow.update();
        popupWindow.showAtLocation(findViewById(R.id.header_right_btn), Gravity.BOTTOM, 0, 0); // 设置窗口显示的位置

        // 拨打电话
        Button cancel = (Button) menuView.findViewById(R.id.btn_study_call_cancel);
        cancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });

        // 拨打电话
        Button call = (Button) menuView.findViewById(R.id.btn_call_phone_num);
        call.setText(_tel);
        call.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
                Intent intent = new Intent(Intent.ACTION_CALL);
                Uri data = Uri.parse("tel:" + _tel);
                intent.setData(data);
                startActivity(intent);
            }
        });
    }

}
